#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

source $(dirname $0)/kvmrc
source $(dirname $0)/../../scripts_common.sh

DRIVER_PATH=$(dirname $0)

DEFAULT_TYPE="raw"

DOMAIN="$1"
SOURCE="$2"
TARGET="$3"
TARGET_INDEX="$4"
DRV_ACTION="$5"

ATTACH_FILE="$SOURCE.attach"

XPATH="${DRIVER_PATH}/../../datastore/xpath.rb -b $DRV_ACTION"

unset i j XPATH_ELEMENTS

DISK_XPATH="/VMM_DRIVER_ACTION_DATA/VM/TEMPLATE/DISK[ATTACH='YES']"

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <($XPATH     /VMM_DRIVER_ACTION_DATA/VM/ID \
                    $DISK_XPATH/DRIVER \
                    $DISK_XPATH/TYPE \
                    $DISK_XPATH/READONLY \
                    $DISK_XPATH/CACHE \
                    $DISK_XPATH/DISCARD \
                    $DISK_XPATH/SOURCE \
                    $DISK_XPATH/DISK_ID \
                    $DISK_XPATH/CLONE \
                    $DISK_XPATH/CEPH_HOST \
                    $DISK_XPATH/CEPH_SECRET \
                    $DISK_XPATH/CEPH_USER \
                    $DISK_XPATH/ISCSI_HOST \
                    $DISK_XPATH/ISCSI_USAGE \
                    $DISK_XPATH/ISCSI_USER \
                    $DISK_XPATH/ISCSI_IQN \
                    $DISK_XPATH/DISK_TYPE \
                    $DISK_XPATH/POOL_NAME)

VMID="${XPATH_ELEMENTS[j++]}"
DRIVER="${XPATH_ELEMENTS[j++]:-$DEFAULT_TYPE}"
TYPE="${XPATH_ELEMENTS[j++]}"
READONLY="${XPATH_ELEMENTS[j++]}"
CACHE="${XPATH_ELEMENTS[j++]}"
DISCARD="${XPATH_ELEMENTS[j++]}"
IMG_SRC="${XPATH_ELEMENTS[j++]}"
DISK_ID="${XPATH_ELEMENTS[j++]}"
CLONE="${XPATH_ELEMENTS[j++]}"
CEPH_HOST="${XPATH_ELEMENTS[j++]}"
CEPH_SECRET="${XPATH_ELEMENTS[j++]}"
CEPH_USER="${XPATH_ELEMENTS[j++]}"
ISCSI_HOST="${XPATH_ELEMENTS[j++]}"
ISCSI_USAGE="${XPATH_ELEMENTS[j++]}"
ISCSI_USER="${XPATH_ELEMENTS[j++]}"
ISCSI_IQN="${XPATH_ELEMENTS[j++]}"
DISK_TYPE="${XPATH_ELEMENTS[j++]}"
POOL_NAME="${XPATH_ELEMENTS[j++]}"

TYPE=$(echo "$TYPE"|tr A-Z a-z)

case "$TYPE" in
block)
    TYPE_SOURCE="dev"
    TYPE_XML="block"
    DEVICE="disk"
    ;;
iscsi)
    TYPE_SOURCE="protocol"
    TYPE_XML="network"
    SOURCE="iscsi"
    DEVICE="disk"

    if [ -n "$ISCSI_IQN" ]; then
        SOURCE_ARGS="name='${ISCSI_IQN}'"
    else
        SOURCE_ARGS="name='${IMG_SRC}'"
    fi

    SOURCE_HOST=$(get_source_xml $ISCSI_HOST)

    if [ -n "$ISCSI_USAGE" -a -n "$ISCSI_USER" ]; then
        AUTH="<auth username='$ISCSI_USER'>\
                <secret type='iscsi' usage='$ISCSI_USAGE'/>\
              </auth>"
    fi
    ;;
cdrom)
    TYPE_SOURCE="file"
    TYPE_XML="file"
    DEVICE="cdrom"
    ;;
rbd*)
    TYPE_SOURCE="protocol"
    TYPE_XML="network"
    SOURCE="rbd"

    if [ "$TYPE" = "rbd_cdrom" ]; then
        DEVICE="cdrom"
    else
        DEVICE="disk"
    fi

    if [ "$CLONE" = "YES" ]; then
        SOURCE_ARGS="name='${IMG_SRC}-${VMID}-${DISK_ID}'"
    else
        SOURCE_ARGS="name='${IMG_SRC}'"
    fi

    SOURCE_HOST=$(get_source_xml $CEPH_HOST)

    if [ -n "$CEPH_USER" -a -n "$CEPH_SECRET" ]; then
        AUTH="<auth username='$CEPH_USER'>\
                <secret type='ceph' uuid='$CEPH_SECRET'/>\
              </auth>"
    fi
    ;;
*)
    #NOTE: This includes TYPE=FS and TYPE=SWAP
    case "$DISK_TYPE" in
    RBD)
        TYPE_SOURCE="protocol"
        TYPE_XML="network"
        SOURCE="rbd"
        DEVICE="disk"

        RBD_SOURCE="${POOL_NAME}/one-sys-${VMID}-${DISK_ID}"

        SOURCE_ARGS="name='${RBD_SOURCE}'"

        SOURCE_HOST=$(get_source_xml $CEPH_HOST)

        if [ -n "$CEPH_USER" -a -n "$CEPH_SECRET" ]; then
            AUTH="<auth username='$CEPH_USER'>\
                    <secret type='ceph' uuid='$CEPH_SECRET'/>\
                  </auth>"
        fi
        ;;
    *)
        TYPE_SOURCE="file"
        TYPE_XML="file"
        DEVICE="disk"
        ;;
    esac

    ;;
esac

[ -z "$CACHE" ] && [ -n "$DEFAULT_ATTACH_CACHE" ] && \
    CACHE="$DEFAULT_ATTACH_CACHE"
[ -n "$CACHE" ] && CACHE="cache='$CACHE'"

[ -z "$DISCARD" ] && [ -n "$DEFAULT_ATTACH_DISCARD" ] && \
    DISCARD="$DEFAULT_ATTACH_DISCARD"
[ -n "$DISCARD" ] && DISCARD="discard='$DISCARD'"

if [ "$READONLY" = "YES" ]; then
    READONLY="<readonly/>"
else
    READONLY=""
fi

cat <<EOF > $ATTACH_FILE
<disk type='$TYPE_XML' device='$DEVICE'>
  <driver name='qemu' type='$DRIVER' $CACHE $DISCARD/>
  <source $TYPE_SOURCE='$SOURCE' $SOURCE_ARGS>
  $SOURCE_HOST
  </source>
  $AUTH
  <target dev='$TARGET'/>
  $READONLY
</disk>
EOF

exec_and_log "virsh --connect $LIBVIRT_URI attach-device $DOMAIN $ATTACH_FILE" \
    "Could not attach $SOURCE ($TARGET) to $DOMAIN"

rm -f "$ATTACH_FILE"
